/*
 * SparkTextViewController
 * @description 本文件提供对话中魔法工具箱中的火星文的viewcontroller
 * @package
 * @author
 * @copyright
 * @version 		1.0
 * @description 本文件提供对话中魔法工具箱中的火星文的viewcontroller
 */

#import "SparkTextViewController.h"
#import "AppDataManager.h"
#import "AppCommunicationManager.h"
#import "TKLoadingView.h"
#import "BanBu_AppDelegate.h"
#import "BanBu_NavButton.h"
#import "BanBu_ChatViewController.h"
#import "BanBu_ImageView.h"

@interface SparkTextViewController ()

@end

@implementation SparkTextViewController

-(void)dealloc
{
    [_textLabel release];
    [_inputView release];
    [_outputView release];
    [super dealloc];
}
-(void)viewWillAppear:(BOOL)animated
{
    [UIView setAnimationsEnabled:YES];
    [super viewWillAppear:animated];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self)
    {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self setUpForDismissKeyboard];
    
    BanBu_ImageView * img1 = [[[BanBu_ImageView alloc] initWithBackGroundImageType:UIImageViewCreatTypeTable]autorelease];
    
    img1.frame = CGRectMake(0, 0, __MainScreen_Width, __MainScreen_Height);
    
    [self.view addSubview:img1];
    
    //发送按钮
    BanBu_NavButton *btn_send=[[BanBu_NavButton alloc] initWithButtonType:UIButtonCreatTypeNormal];
    btn_send.position = ButtonPositionRight;
    btn_send.frame=CGRectMake(0, 0, 50, 30);
    [btn_send setTitle:NSLocalizedString(@"sendReply", nil) forState:UIControlStateNormal];
    [btn_send addTarget:self action:@selector(sendMsg) forControlEvents:UIControlEventTouchUpInside];
    btn_send.titleLabel.font = [UIFont systemFontOfSize:14];
    
    self.navigationItem.rightBarButtonItem=[[[UIBarButtonItem alloc]initWithCustomView:btn_send]autorelease];
    
    //文本输入框
    _inputView = [[UITextView alloc] initWithFrame:CGRectMake(10, 10, 300,85)];
    _inputView.textColor = [UIColor blackColor];
    self.inputView.font = [UIFont systemFontOfSize:18];
    _inputView.layer.borderColor = [[UIColor grayColor] CGColor];
    _inputView.layer.borderWidth = 1.0;
    _inputView.layer.cornerRadius = 4.0;
    _inputView.delegate = self;
    self.inputView.returnKeyType = UIReturnKeyDefault;
    self.inputView.keyboardType = UIKeyboardTypeDefault;
    [self.view addSubview:self.inputView];
    [_inputView release];
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(5, 5, 300,25)];
    label.text = NSLocalizedString(@"请在此输入需要转换的文字...", nil);
    label.textAlignment = UITextAlignmentLeft;
    label.numberOfLines = 1;
    label.textColor = [UIColor darkGrayColor];
    label.alpha = 0.6;
    _textLabel = label;
    [_inputView addSubview:_textLabel];
    [label release];
    
    UIButton *creatButton = [UIButton buttonWithType:UIButtonTypeCustom];
    creatButton.frame = CGRectMake(50, 110, 120, 40);
    [creatButton setBackgroundImage:[UIImage imageNamed:@"creatspark.png"] forState:UIControlStateNormal];
    [creatButton addTarget:self action:@selector(creatSparkText) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:creatButton];
    
    UIButton *copyButton = [UIButton buttonWithType:UIButtonTypeCustom];
    copyButton.frame = CGRectMake(230,110, 40, 40);
    [copyButton.layer setMasksToBounds:YES];
    [copyButton setBackgroundImage:[UIImage imageNamed:@"copybut.png"] forState:UIControlStateNormal];
    copyButton.layer.borderColor = [[UIColor blackColor] CGColor];
    [copyButton addTarget:self action:@selector(copyText) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:copyButton];
    
    _outputView = [[UITextView alloc] initWithFrame:CGRectMake(10, 165, 300,__MainScreen_Height-44-165-10)];
    _outputView.textColor = [UIColor blackColor];
    self.outputView.font = [UIFont systemFontOfSize:18];
    _outputView.layer.borderColor = [[UIColor grayColor] CGColor];
    _outputView.layer.borderWidth = 1.0;
    _outputView.layer.cornerRadius = 4.0;
    _outputView.editable = NO;
    self.outputView.returnKeyType = UIReturnKeyDefault;
    self.outputView.keyboardType = UIKeyboardTypeDefault;
    [self.view addSubview:self.outputView];
    [_outputView release];
    
    UIActivityIndicatorView *acity = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(0, 0, 80, 80)];
    acity.hidesWhenStopped = YES;
    acity.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhiteLarge;
    acity.color = [UIColor darkGrayColor];
    acity.center = _outputView.center;
    _activity = acity;
    [self.view addSubview:acity];
    [acity release];

}

-(void)copyText
{
    [_inputView resignFirstResponder];
    if(_outputView.text.length)
    {
        UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
        [pasteboard setString:_outputView.text];
        [TKLoadingView showTkloadingAddedTo:self.view title:@"内容已复制\n请粘贴到QQ、微博、陌陌等！" activityAnimated:NO duration:2.0];
    }
    else
    {
        [TKLoadingView showTkloadingAddedTo:self.view title:@"复制信息不能为空" activityAnimated:NO duration:1];
    }
}

//发送按钮
-(void)sendMsg
{
    if(!_inputView.text.length)
    {
        [TKLoadingView showTkloadingAddedTo:self.view title:@"发送信息不能为空" activityAnimated:NO duration:1];
    }
    else if(_inputView.text.length&&!_outputView.text.length)
    {
        [TKLoadingView showTkloadingAddedTo:self.view title:@"请先点击按钮生成火星文" activityAnimated:NO duration:1];
    }
    else //发送文本
    {
        //发送广播通知；
        NSDictionary * sendDic = [NSDictionary dictionaryWithObjectsAndKeys:_outputView.text,KeyContent,[NSNumber numberWithInt:ChatCellTypeText],KeyType,@"mg",KeyShowFrom, nil];
        [[NSNotificationCenter defaultCenter]postNotificationName:kSendMsgAllTypeNotification object:sendDic];
        [self popToChat];
    }
}

-(void)textViewDidChange:(UITextView *)textView
{
    _textLabel.hidden = _inputView.text.length?YES:NO;
}

-(void)creatSparkText
{
    [_inputView resignFirstResponder];
    [_activity startAnimating];
    if(_inputView.text.length)
    {
        NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:_inputView.text,@"huoxing", nil];
        [AppComManager getBanBuData:BanBu_Get_Huoxing_text par:dic delegate:self];
    }
}

-(void)popToChat
{
    [self.navigationController popToViewController:self.chat animated:YES];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)banbuRequestDidFinished:(NSDictionary *)resDic error:(NSError *)error
{
    [_activity stopAnimating];
    if (error)
    {
        [TKLoadingView showTkloadingAddedTo:self.view title:@"数据获取失败" activityAnimated:YES duration:1];
    }
    else
    {
        if([[resDic valueForKey:@"ok"]isEqual:@"y"])
        {
            _outputView.text = [resDic valueForKey:@"huoxing"];
        }
    }   
}
- (void)setUpForDismissKeyboard
{
    NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
    UITapGestureRecognizer *singleTapGR =
    [[UITapGestureRecognizer alloc] initWithTarget:self
                                            action:@selector(tapAnywhereToDismissKeyboard:)];
    NSOperationQueue *mainQuene =[NSOperationQueue mainQueue];
    [nc addObserverForName:UIKeyboardWillShowNotification
                    object:nil
                     queue:mainQuene
                usingBlock:^(NSNotification *note){
                    [self.view addGestureRecognizer:singleTapGR];
                }];
    [nc addObserverForName:UIKeyboardWillHideNotification
                    object:nil
                     queue:mainQuene
                usingBlock:^(NSNotification *note){
                    [self.view removeGestureRecognizer:singleTapGR];
                }];
}

- (void)tapAnywhereToDismissKeyboard:(UIGestureRecognizer *)gestureRecognizer
{
    [self.view endEditing:YES];
}

@end
